package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"product-module/pkg"
	"product-module/product-api/client"
	"product-module/product-api/request"
	"product-module/product-rpc/proto/good"
)

func GoodCreate(c *gin.Context) {
	var data request.GoodCreate
	fmt.Println(data)
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "未接收到该参数",
			"data": nil,
		})
		return
	}
	create, err := client.GoodCreate(c, &good.GoodCreateReq{
		GoodName:     data.GoodName,
		Image:        data.Image,
		Video:        data.Video,
		GoodCode:     data.GoodCode,
		SellingPoint: data.SellingPoint,
		Inventory:    data.Inventory,
		SalesVolume:  data.SalesVolume,
		GoodType:     data.GoodType,
		PageView:     data.PageView,
		Status:       data.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "商品发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "商品发布成功",
		"data": create,
	})
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"mag":  "未接收到参数",
			"data": nil,
		})
		return
	}
	ext := file.Filename
	if ext != ".mp4" && ext != ".jpg" && ext != ".png" {
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "上传的格式不正确",
			"data": nil,
		})
		return
	}
	dst := "D:\\GoWork\\zg5\\rk\\product-module\\product-api\\images\\img\\" + file.Filename
	pkg.OoSo()
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "文件上传成功",
			"data": nil,
		})
		return
	}
	url := "/image/img/"
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "文件上传成功",
		"data": url,
	})
}
